﻿#include "statisticsanalyze.h"
#include <assert.h>
#include <QDebug>
#include "EagleMainFrame.h"
#include "ui_diagramDlg.h"


#ifdef _WIN32
#ifdef _DEBUG
#ifdef STATIC_LIB
#pragma comment(lib,"qcustomplotd.lib")
#else
#pragma comment(lib,"qcustomplotd2.lib")
#endif
#else
#pragma comment(lib,"qcustomplot2.lib")
#endif
#endif

#pragma execution_character_set("utf-8")

#if 1

StatisticsAnalyze::StatisticsAnalyze(QWidget *parent, QPoint& start, QPoint& end, int w, int h, int nDepth, uchar* buf)
: QDialog(parent), ui(new Ui::diagram)
{
	ui->setupUi(this);
	_RIO = new RIOManager(start, end, w, h, nDepth, buf);
	QRect rec = _RIO->GetSubRIO();
	
	int colors = pow(2.0, _RIO->GetRIOBitWidth());
	diagram = new unsigned int[colors];
	memset(diagram, 0, colors * sizeof(unsigned int));
	int _min = 65535;
	int _max = 0;
	unsigned long long _sum = 0;
	unsigned long long _var = 0;
	int pix_num = 0;

	int sw = _RIO->GetRIOCols(), sh = _RIO->GetRIORows();
	for (int i = 0; i < sh; i++)
	{
		for (int j = 0; j < sw; j++)
		{
			int value = _RIO->GetRIOValue(j, i);
			_min = _min < value ? _min : value;
			_max = _max > value ? _max : value;
			_sum += value;
			assert(value >= 0);
			diagram[value % colors] += 1;
			pix_num++;
		}
	}

	for (int i = 0; i < colors; i++)
	{
		//if (diagram[i] != 0)
		{
			x_coord << i;
			y_coord << diagram[i];
		}
	}

	min = _min;
	max = _max;
	avg = _sum / pix_num;

	// 方差
	for (int i = rec.top(); i < rec.bottom(); i++){
		for (int j = rec.left(); j < rec.right(); j++){
			int value = _RIO->GetValue(j, i);
			_var += (value - avg) * (value - avg);
		}
	}
	var = (int)(_var / pix_num);

	ui->x->setText(QString::number(rec.left()));
	ui->y->setText(QString::number(rec.top()));
	ui->w->setText(QString::number(rec.width() +1));
	ui->h->setText(QString::number(rec.height() + 1));
	ui->min->setText(QString::number(min));
	ui->max->setText(QString::number(max));
	ui->average->setText(QString::number(avg));
	ui->variance->setText(QString::number(qSqrt(var),'g',2));

	ui->customPlot->xAxis->setBasePen(QPen(Qt::red, 2));
	ui->customPlot->xAxis->setLabel(tr("灰度值"));
	ui->customPlot->xAxis->setLabelColor(Qt::blue);
	ui->customPlot->yAxis->setBasePen(QPen(Qt::red, 2));
	ui->customPlot->yAxis->setLabel(tr("像素点个数"));
	ui->customPlot->yAxis->setLabelColor(Qt::blue);

	ui->customPlot->addGraph();
	ui->customPlot->graph(0)->setData(x_coord, y_coord);
	ui->customPlot->graph()->setPen(QPen(Qt::green));
	ui->customPlot->rescaleAxes();
	// 支持 拖拽及缩放
	ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

// for Test [12/8/2016 ejitec]
StatisticsAnalyze::StatisticsAnalyze(QWidget *parent)
: QDialog(parent), ui(new Ui::diagram), _RIO(nullptr)
{
	ui->setupUi(this);
}

void StatisticsAnalyze::LoadTestDiagram()
{
	// create plot (from quadratic plot example):
	QVector<double> x(101), y(101);
	for (int i = 0; i < 101; ++i)
	{
		x[i] = i / 50.0 - 1;
		y[i] = x[i] * x[i];
	}
	ui->customPlot->addGraph();
	ui->customPlot->graph(0)->setData(x, y);
	ui->customPlot->graph()->setPen(QPen(Qt::green));
	ui->customPlot->xAxis->setBasePen(QPen(Qt::red,2));
	ui->customPlot->xAxis->setNumberPrecision(1000);
	ui->customPlot->xAxis->setLabel(tr("灰度值"));
	ui->customPlot->xAxis->setLabelColor(Qt::blue);
	ui->customPlot->yAxis->setBasePen(QPen(Qt::red, 2));
	ui->customPlot->yAxis->setLabel(tr("像素点个数"));
	ui->customPlot->yAxis->setLabelColor(Qt::blue);
	ui->customPlot->rescaleAxes();
	ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}


StatisticsAnalyze::~StatisticsAnalyze()
{
	delete _RIO;
}

#endif

RIOManager::RIOManager(QPoint& start, QPoint& end, int w, int h, int nDepth, uchar* buf)
:_startP(start), _endP(end)
, _columns(w), _lines(h)
, _bit_width(nDepth)
, _buf(nullptr)
{
	if (_startP == _endP)
	{
		_startP = QPoint(0, 0);
		_endP = QPoint(w - 1, h -1);
	}

	QRect tmp = QRect(_startP, _endP);
	_startP = tmp.center() - QPoint((qAbs(tmp.width()) / 2 - 1), qAbs(tmp.height()) / 2 - 1);
	_endP = tmp.center() + QPoint(qAbs(tmp.width() / 2), qAbs(tmp.height()) / 2);

	rio_columns = qAbs(tmp.width());
	rio_rows = qAbs(tmp.height());


	qDebug() << "W,H:" << rio_columns << " " << rio_rows << " " << "Center:"
		<< tmp.center();

	_buf = new unsigned char[w * h * ((nDepth + 7) / 8)];
	memcpy(_buf, buf, h * w * ((nDepth + 7) / 8));
}

RIOManager::~RIOManager()
{
	if (_buf)
	{
		delete[] _buf;
	}

	qDebug() << "RIOManager::~RIOManager()";
}
